package cn.com.surker.system.service.impl;

import cn.com.surker.system.dao.SysMessageRecordDao;
import cn.com.surker.system.entity.SysMessageRecord;
import cn.com.surker.system.service.SystemMessageService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lucky
 * @create 2022年07月27 14:15
 * @desc
 */
@Service
public class SystemMessageServiceImpl implements SystemMessageService {

    @Resource
    private SysMessageRecordDao mapper;

    /**
     * 批量新增
     * @param list
     * @return
     */
    public int batchSave(List<SysMessageRecord> list) {
        if (list == null || list.size() == 0)
            return -1;

        for (int i = 0; i < list.size(); i++) {
            mapper.insert(list.get(i));
        }
        return 0;
    }

    /**
     * 根据条件查询所有的数据
     * @param dto
     * @return
     */
    public List<SysMessageRecord> findAll(SysMessageRecord dto){

        QueryWrapper<SysMessageRecord> query = new QueryWrapper();
        query.setEntity(dto);
        List<SysMessageRecord> list = mapper.selectList(query);

        return list;
    }


    /**
     * 分页查询数据
     * @param phone 电话
     * @param searchValue 检索条件
     * @param type
     * @param startTime
     * @param endTime
     * @param offset
     * @param limit
     * @return
     */
    public IPage findAllByPage(String phone, String searchValue, Integer type,
                                       String startTime, String endTime, Integer offset, Integer limit){
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<SysMessageRecord> query = new QueryWrapper();

        if (!StringUtils.isNull(startTime))
            query.ge("create_time", startTime);
        if (!StringUtils.isNull(endTime))
            query.lt("create_time", endTime);
        if (!StringUtils.isNull(phone)) {
            query.and(messageQueryWrapper -> messageQueryWrapper.like("openid", phone).or()
                    .like("phone", phone).or().like("username", phone));
        }
        if (!StringUtils.isNull(searchValue)) {
            query.and(messageQueryWrapper -> messageQueryWrapper.like("title", searchValue).or()
                    .like("content", searchValue));
        }
        if (!StringUtils.isNull(type))
            query.eq("send_type", type);

        IPage<SysMessageRecord> iPage = mapper.selectPage(new Page<>((offset + limit) /limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }


    /**
     * 新增数据
     * @param dto page
     * @return
     */
    public int saveInit(SysMessageRecord dto){
        int saveStatus = mapper.insert(dto);
        if(saveStatus > 0){
            return 0;
        }else{
            return -1;
        }

    }

    /**     * 更新数据
     * @param dto page
     * @return
     */
    public int update(SysMessageRecord dto){
        if (dto == null || StringUtils.isNull(dto.getId()))
            return -1;
        int saveStatus = mapper.updateById(dto);
        if(saveStatus > 0){
            return 0;
        }else{
            return -1;
        }

    }

}
